﻿#define compile
//#define testvalidation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using DtsExplorerProject.Helpers;
using System.Xml.Linq;

namespace DtsExplorerProject
{
    static public class DtsResourcesManager
    {

        static public string FilePath { get; set; }
        static public IEnumerable<Configuration> Data(string filename,string rootNode)
        {
            List<Configuration> data = new List<Configuration>();
#if testvalidation
            bool validationResult = SchemaValidator.ValidateDtsSchema(filename);


            if (validationResult == false)
            {
                throw new ArgumentException();
            }
#endif
            //Here put the logic that reads from xml files

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(FilePath);

            XmlNodeList nodeList = xDoc.SelectNodes("DTSConfiguration/Configuration");

            Configuration configuration;
            

            foreach (XmlNode node in nodeList)
            {
                if (node.Name.Equals("Configuration"))
                {
                    configuration = new Configuration
                    {
                        ConfiguredType = node.Attributes["ConfiguredType"].Value,
                        ConfiguredValue = node.SelectSingleNode("ConfiguredValue").InnerText,
                        Path = node.Attributes["Path"].Value,
                        ValueType = node.Attributes["ValueType"].Value               
                    };

                    data.Add(configuration);
                }
            } 
            return data;
        }
        static public PackageInfo GetPackageInfo(string filename,string xpathexpression)
        {
            PackageInfo info = new PackageInfo();

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(FilePath);

            XmlNode root = xDoc.SelectSingleNode(@"DTSConfiguration/DTSConfigurationHeading/DTSConfigurationFileInfo");

            info.Owner = root.Attributes["GeneratedBy"].Value.ToString();
            info.CreationDate = root.Attributes["GeneratedDate"].Value.ToString();
            info.PacakgeName = root.Attributes["GeneratedFromPackageName"].Value.ToString();
            info.PacakgeID = root.Attributes["GeneratedFromPackageID"].Value.ToString();

            return info;
        }

        static public bool UpdateValueNode(string filename,params string[] values)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(FilePath);
            XmlNodeList nodeList = xDoc.SelectNodes(@"DTSConfiguration/Configuration");
            foreach (XmlNode item in nodeList)
            {
                bool value1 = item.Attributes["ConfiguredType"].Value == values[0];
                bool value2 = item.Attributes["Path"].Value==values[1];
                bool value3 = item.Attributes["ValueType"].Value == values[2];

                bool test = value1 && value2 && value3;

                if (test)
                {
                    item.ChildNodes[0].InnerText = values[3].ToString();
                    xDoc.Save(FilePath);
                }   
            }
            
            return true;
        }
    }
}
